package com.learn.project2.service.wx.auth;

import com.learn.project2.bean.MarketUser;
import com.learn.project2.bean.MarketUserExample;
import com.learn.project2.config.customException.RealmFailException;
import com.learn.project2.mapper.MarketUserMapper;
import com.learn.project2.model.bo.wx.auth.AuthLoginBO2;
import com.learn.project2.model.bo.wx.auth.AuthRegisterBO;
import com.learn.project2.model.vo.wx.auth.AuthLoginVO2;
import com.learn.project2.utils.SecretUtils;
import com.learn.project2.utils.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * \* Author: Valley
 * \* Date: 2022/1/8
 * \* Time: 21:05
 * \* Description:
 */
@Service
public class AuthServiceImpl2 implements AuthService2 {

    @Autowired
    MarketUserMapper userMapper;

    /**
     * @description: 账号登录功能
     */
    @Override
    public AuthLoginVO2 authLogin(AuthLoginBO2 loginBO, HttpServletRequest request) throws Exception {

        HttpSession session = request.getSession();
        Subject subject = SecurityUtils.getSubject();
        MarketUser principal = (MarketUser) subject.getPrincipal();
        principal.setLastLoginTime(StringUtils.dateToDate(new Date()));
        principal.setLastLoginIp(request.getRemoteAddr());
        int i = userMapper.updateByPrimaryKey(principal);
        MarketUserExample userExample = new MarketUserExample();
        if ("Mac OS X".equals(System.getProperty("os.name"))){
            userExample.createCriteria()
                    .andUsernameEqualTo(loginBO.getUsername()).andDeletedEqualTo(false);
        }else {
            userExample.createCriteria()
                    .andUsernameEqualTo(loginBO.getUsername())
                    .andPasswordEqualTo(SecretUtils.enAESAndMD5(loginBO.getPassword())).andDeletedEqualTo(false);
        }
        List<MarketUser> users = userMapper.selectByExample(userExample);
        if (users == null || users.size() <= 0) throw new RealmFailException();
        MarketUser user = users.get(0);
        AuthLoginVO2 authLoginVO2 = new AuthLoginVO2();
        AuthLoginVO2.UserInfoBean userInfoBean = new AuthLoginVO2.UserInfoBean();
        userInfoBean.setNickName(user.getNickname());
        userInfoBean.setAvatarUrl(user.getAvatar());
        authLoginVO2.setUserInfo(userInfoBean);
        authLoginVO2.setToken(session.getId());
        return authLoginVO2;
    }

    @Override
    public Integer authLogout(Subject subject) {
        MarketUser user = (MarketUser) subject.getPrincipal();
//        userMapper.updateByPrimaryKey();
        return 200;
    }
    /**
     * @description: 注册
     * @param:
     * @return:
     * @author 亓永杰
     * @date: 2022-01-12 22:57
     */
    @Override
    public void authRegister(AuthRegisterBO authRegisterBO) throws Exception {
        MarketUser user = new MarketUser();
        user.setUsername(authRegisterBO.getUsername());
        user.setPassword(SecretUtils.enAESAndMD5(authRegisterBO.getPassword()));
        user.setMobile(authRegisterBO.getMobile());
        user.setAddTime(new Date());
        user.setUpdateTime(new Date());
        user.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        user.setNickname(authRegisterBO.getUsername());
        userMapper.insertSelective(user);
    }




}
